package com.wf.day_0604.test1.pojo;

import com.wf.day_0604.test1.Demo;
import com.wf.day_0604.test1.exception.DeleteException;
import com.wf.day_0604.test1.exception.InsertException;
import com.wf.day_0604.test1.exception.UpdateException;
import com.wf.day_0604.test1.pojo.Book;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author mpk
 * @since 2024/6/4 17:47:54
 */
public class SysManager {


    List<Book> books = new ArrayList<>();

    public void showAll() {

        for (Book book : books) {
            System.out.println(book.toString());
        }
    }


    public void addBook(Book book) throws InsertException {

        if (books.contains(book)) {
            throw new InsertException("添加书籍重复!失败！");
        }
        books.add(book);
        System.out.println("添加书籍成功");
    }


    public void removeBook(Book book) throws DeleteException {

        if (!books.remove(book)) {
            throw new DeleteException("删除书籍失败!,书籍中无此书!");
        }

        System.out.println("删除书籍成功!");

    }

    public void updateBook(Book book) throws UpdateException, InsertException, DeleteException {


        Boolean exit = isExit(book);
        if (!exit) {
            throw new UpdateException("书籍中无此书！！！");
        }
        else {
            removeBook(book);

            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入书籍信息：");
            Book book1 = new Book(scanner.next(), scanner.next(), scanner.nextDouble());
            books.add(book1);
            System.out.println("修改书籍成功");
        }


    }

    public Boolean isExit(Book book) {

        for (Book book1 : books) {
            if (book1.equals(book)) {
                return true;
            }

        }
        return false;


    }
}